package com.xintonghua.bussiness.ui.user.log;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.xintonghua.base.widget.ShowAllGridView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.AddPhotosAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.event.LogRefreshEventBus;
import com.xintonghua.bussiness.util.ImageTools;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.RegExpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.iwf.photopicker.PhotoPicker;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user.log
 */

public class AddLogActivity extends BaseActivity implements AddPhotosAdapter.OnClickAddListener {
    @BindView(R.id.gv_log_photos)
    ShowAllGridView gvLogPhotos;
    @BindView(R.id.edt_log_content)
    EditText edtLogContent;
    private AddPhotosAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_log);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("添加日志", "发布", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(getStr(edtLogContent))) {
                    mToast("请输入分享日志内容");
                    return;
                }
                if (RegExpUtils.containsEmoji(getStr(edtLogContent))) {
                    mToast("请勿输入表情内容");
                    return;
                }
                String img = MyUtils.getStringSplitValue(adapter.getDataList());
                httpCent.saveGrowthLog(getStr(edtLogContent),img,AddLogActivity.this,MyState.requestTwo);
            }
        });
        adapter = new AddPhotosAdapter(this, 9, this);
        gvLogPhotos.setAdapter(adapter);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                List<String> list = (List<String>) data;
                adapter.addAllData(list);
                break;
            case MyState.requestTwo:
                mToast("发布成功！");
                MyUtils.hideSoftInput(this);
                EventBus.getDefault().post(new LogRefreshEventBus());
                finish();
                break;
        }
    }

    @Override
    public void onClickAdd(int position) {
        MyUtils.startPhotoPicker(this, 9 - adapter.getPhotoCount(), 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 100:
                    if (data != null) {
                        ArrayList<String> photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                        if (photos != null) {
                            ImageTools.loadImages(this, photos, this, MyState.requestOne);
                        }
                    }
                    break;
            }
        }
    }
}
